% set the paramers as in Table 2

%% parameters governing the grid for debt and time
dt      = .01;%time in years
db      = 0.025;%change in b
T       = ceil(20/dt);
n_sim   = 500000;


%% load data age profiles of Figure 7 in the main text
%data_age = readmatrix('AgeProfile.txt');

load data_age.mat
age=data_age(1:18,2);
b_targ_north =data_age(1:18,7);
b_targ_south =data_age(19:end,7);
b_targ_north_bank = data_age(1:18,6);
b_targ_south_bank = data_age(19:end,6);
exit_targ_north      =  data_age(1:18,3);
exit_targ_south      =  data_age(19:end,3);
bankrpcy_targ_north  = data_age(1:18,4);
bankrpcy_targ_south  = data_age(19:end,4);
size_targ_north      =  data_age(1:18,10);
size_targ_south      =  data_age(19:end,10);
prod_targ_north      =  data_age(1:18,5);
prod_targ_south      =  data_age(19:end,5);

a_targ  = 0.2; %Target bankruptcy recovery rate, north and south
delta_n = mean(exit_targ_north -bankrpcy_targ_north);% Entrepreneur death rate, north
delta_s = mean(exit_targ_south -bankrpcy_targ_south);% Entrepreneur death rate, south

%compute the Idiosyncratic risk,
sigma_south     = 0.31;
sigma_north     = 0.25;

%set other parameters ex-ante
nu      = 4.5;%demand elasticity
r       = 0.03;%Firm discount rate, 
r_c     = 0.011;%Financiers discount rate,
dr      = r-r_c;
rbar_ben= r-dr;
rbar    = rbar_ben;
rho     = 1/30; %Debt maturity arrival rate
theta   = 2; %Curvature adjustment cost function,
wei_s   = 0.35; % Population size, south
wei_n   = 1-wei_s; % Population size, north
m_n_ss = 0.05; %mass of firms per worker, north 
m_s_ss = 0.09;%mass of firms per worker,  south
l_s=1;%mass of workers, south
l_n=1;%mass of workers, north
l_s_ss=l_s;
l_n_ss=l_n;
eta=3.0;%Congestion elasticity to workforce

%% compute several calibration targets and statistics
y_s   = 0.66; % Average labor productivity target, south
y_n   = (1-y_s*l_s*wei_s)/(l_n*wei_n);% Average labor productivity target, north
Y     = y_s*l_s*wei_s+y_n*l_n*wei_n;%Aggregate output
w     = (nu-1)/nu; %aggregate wage rate
w_n   = y_n*(nu-1)/nu; % wage rate, north
w_s   = y_s*(nu-1)/nu; % wage rate, south
A_s       = Y* (w_s*nu/(nu-1))^(1-nu);%value added  per unit of Z
A_n       = Y* (w_n*nu/(nu-1))^(1-nu);%value added  per unit of Z
A_s_ben=A_s;
A_n_ben=A_n;
Profit_s  = A_s/nu;%profit per unit of Z
Profit_n  = A_n/nu;%profit per unit of Z
sub_tax_s = y_s*l_s*wei_s/(y_s*l_s*wei_s+y_n*l_n*wei_n);%tax rate on South workers: share of south GDP
crit_h=1;
h_s=0.0231;
while crit_h>1/1000
    h_n   = (w_n-w_s+h_s*l_s^eta)/(l_n^eta);
    %h_s_n = (0.33*(w_s*l_s*wei_s+w_n*l_n*wei_n)-wei_n*h_n*l_n^(1+eta))/(wei_s*l_s^(1+eta));
    h_s_n = (0.33*(y_s*l_s*wei_s+y_n*l_n*wei_n)-wei_n*h_n*l_n^(1+eta))/(wei_s*l_s^(1+eta));
    crit_h= abs(h_s_n-h_s);
    h_s   = h_s_n.*.5+h_s*.5;
end
beta_hat=1/eta*w_s/(h_s*l_s^(eta)); %Long-run elasticity of workforce to wages

